﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using OBID;

namespace RFIDReadClass
{
    class ReadClass : FeIscListener
    {

        public delegate void DelegateOnRecProtocol(byte[] protocol);
        

        private FedmIscReader reader;
        private DelegateOnRecProtocol delOnRecProtocol;

        public ReadClass()
        {
            try
            {
                reader = new FedmIscReader();
                delOnRecProtocol = new DelegateOnRecProtocol(DisplayReceiveProtocol);
                
            }
            catch (FedmException e)
            {
                System.Console.WriteLine(e.ToString());
                Application.Exit();
            }
        }


        public Boolean Connected()
        {

            return reader.Connected;
        }

        public string GetReaderID()
        {

            return reader.GetPortPara("Device-ID");
        }

            public void StartReader()
        {
            try
            {
                reader.ConnectUSB(0);

                reader.ReadReaderInfo(); // read important reader info
                
                reader.ReadCompleteConfiguration(true); // read reader's configuration from EEPROM (this need some time!)
                byte ucReaderMode;
                                 
                
               // MessageBox.Show(reader.GetReaderType().ToString());
                reader.SetConfigPara(OBID.ReaderConfig.OperatingMode.Mode, (byte)1, true);

                // formatted ascii
                reader.SetConfigPara(OBID.ReaderConfig.OperatingMode.ScanMode.DataFormat.Format, (byte)0, true); // formatted ascii
                
                //Liest einmal bis ein Transponder erneut das event auslöst
                reader.SetConfigPara(OBID.ReaderConfig.OperatingMode.ScanMode.Trigger.Mode, (byte)0, true); // formatted ascii
               
                
                reader.ApplyConfiguration(true);
                //Scan-Mode
                reader.GetConfigPara(OBID.ReaderConfig.OperatingMode.Mode, out ucReaderMode, true);
                MessageBox.Show("Mode "+ucReaderMode.ToString());
                
                reader.GetConfigPara(OBID.ReaderConfig.OperatingMode.ScanMode.Trigger.Mode, out ucReaderMode, true);
                MessageBox.Show("Trigger.mode " + ucReaderMode.ToString());

                reader.GetConfigPara(OBID.ReaderConfig.OperatingMode.ScanMode.Trigger.Enable, out ucReaderMode, true);
                MessageBox.Show("Trigger.enable " + ucReaderMode.ToString());


                // add the listener
                reader.AddEventListener(this, FeIscListenerConst.SCANNER_PRT_EVENT);
               
            }
            catch (Exception)
            {
                MessageBox.Show("Nicht geklappt");
                if (reader.Connected)
                {
                    try
                    {
                        StopReader();
                        
                    }
                    catch (Exception) { }
                }
                
                return;
            }
        }

        public void StopReader()
        {
            // remove the listener

            //Set host-Mode and stop Scan-Mode
            reader.SetConfigPara(OBID.ReaderConfig.OperatingMode.Mode, (byte)0, true);

            reader.ApplyConfiguration(true);
            reader.RemoveEventListener(this, FeIscListenerConst.SCANNER_PRT_EVENT);
            reader.DisConnect();
        }
        

        /// <summary>
        /// Die verwendeten Ressourcen bereinigen.
        /// </summary>
        /// 
        public void Close(bool disposing)
        {
            if (disposing)
            {
                if (reader.Connected)
                {
                    try
                    {
                        // remove the listener
                        StopReader();
                    }
                    catch (Exception) { }
                }
            }
        }


        public void OnSendProtocol(FedmIscReader reader, byte[] sendProtocol)
        {
            int i;
        }

        public void OnReceiveProtocol(FedmIscReader reader, byte[] receiveProtocol)
        {
            //            DelegateOnRecProtocol delOnRecProtocol = new DelegateOnRecProtocol(DisplayReceiveProtocol);
            delOnRecProtocol(receiveProtocol);
            //this.Invoke(delOnRecProtocol, receiveProtocol);
        }

        public void DisplayReceiveProtocol(byte[] receiveProtocol)
        {
            string data = "";

            //if (this.comboAscii.SelectedIndex == 1)
            //{
                for (int i = 0; i < receiveProtocol.Length; i++)
                {
                    data += (char)receiveProtocol[i];
                }
            //}
            //else
            //{
            //    data = FeHexConvert.ByteArrayToHexStringWithSpaces(receiveProtocol);
            //    data += "\r\n";
            //}
            //this.dataWindow.AppendText(data);
                MessageBox.Show("byte " +data);
        }

        public void DisplayReceiveProtocol(string receiveProtocol)
        {
            string data = receiveProtocol;

            
            //if (this.comboAscii.SelectedIndex == 1)
            //{
            //for (int i = 0; i < receiveProtocol.Length; i++)
            //{
            //    data += (char)receiveProtocol[i];
            //}
            //}
            //else
            //{
            //    data = FeHexConvert.ByteArrayToHexStringWithSpaces(receiveProtocol);
            //    data += "\r\n";
            //}
            //this.dataWindow.AppendText(data);
            MessageBox.Show("string: " + data);
        }

        public void OnSendProtocol(FedmIscReader reader, string sendProtocol)
        {
            return;
        }

        public void OnReceiveProtocol(FedmIscReader reader, string receiveProtocol)
        {
            DisplayReceiveProtocol(receiveProtocol);
            return;
        }



    }
}
